﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace MS.INV
{
    public partial class Banner : Control
    {
        public event EventHandler SupportClicked;

        Timer tmr = new Timer();
        Timer tmrChk = new Timer();

        float iOpac = 0;
        int Pause = 0;
        int index = 1;

        private string _LText = "";
        public string LargeText
        {
            get { return _LText; }
            set { _LText = value; }
        }
        private string _SText = "";
        public string SmallText
        {
            get { return _SText; }
            set { _SText = value; }
        }

        GraphicsPath L_Path = null;
        GraphicsPath L_OffPath = null;
        GraphicsPath S_Path = null;
        GraphicsPath S_OffPath = null;

        Rectangle img_Help = Rectangle.Empty;
        Rectangle img_Supp = Rectangle.Empty;
        Rectangle img_logout = Rectangle.Empty;

        public Banner()
        {
            InitializeComponent();
            this.BackgroundImageLayout = ImageLayout.Zoom;

            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            tmr.Interval = 200;
            tmr.Tick += new EventHandler(tmr_Tick);
            tmr.Start();

            tmrChk.Interval = 1000;
            tmrChk.Tick += new EventHandler(tmrChk_Tick);
        }

        void tmrChk_Tick(object sender, EventArgs e)
        {
            if (Pause >= 6000)
            {
                iOpac = 0;
                Pause = 0;
                index++;

                tmr.Start();
                tmrChk.Stop();
            }
            Pause += tmrChk.Interval;
        }

        void tmr_Tick(object sender, EventArgs e)
        {
            if (iOpac > 1)
            {
                Pause = 0;
                this.BackgroundImage = GetImage();
                tmrChk.Start();
                tmr.Stop();
                return;
            }
            if (index > 3) index = 1;

            iOpac += 0.1F;
            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;

            g.DrawImage(SetImgOpacity(iOpac), this.ClientRectangle);

            g.SmoothingMode = SmoothingMode.AntiAlias;
            using (Brush b = new SolidBrush(Color.FromArgb(100, 0, 0, 0)))
            {
                g.FillPath(b, L_OffPath);
                g.FillPath(b, S_OffPath);
            }
            
            g.DrawPath(Pens.SkyBlue, L_Path);
            g.DrawPath(Pens.Gray, S_Path);

            g.DrawImage(Properties.Resources.Help, img_Help);
            g.DrawImage(Properties.Resources.Messages, img_Supp);
            g.DrawImage(Properties.Resources.Logout, img_logout);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            img_logout = new Rectangle(this.Width - 50, this.Height / 2 - 32 / 2, 32, 32);
            img_Help = new Rectangle(this.Width - 90, this.Height / 2 - 32 / 2, 32, 32);
            img_Supp = new Rectangle(this.Width - 130, this.Height / 2 - 32 / 2, 32, 32);

            using (Graphics g = this.CreateGraphics())
            {
                RectangleF rect = new Rectangle(0, 0, 200, this.Height);
                L_Path = GetStringPath(LargeText, g.DpiY, rect, this.Font, StringFormat.GenericTypographic);
                rect.Offset(4, 4);
                L_OffPath = GetStringPath(LargeText, g.DpiY, rect, this.Font, StringFormat.GenericTypographic);

                using (Font f = new Font(this.Font.Name, 10F, FontStyle.Bold))
                {
                    Rectangle s_rect = new Rectangle(20, 30, 500, 25);

                    S_Path = GetStringPath(_SText, g.DpiY, s_rect, f, StringFormat.GenericTypographic);
                    s_rect.Offset(3, 3);
                    S_OffPath = GetStringPath(_SText, g.DpiY, s_rect, f, StringFormat.GenericTypographic);
                }
            }
        }

        private Image GetImage()
        {
            if (index == 1)
                return MS.INV.Properties.Resources._1;
            else if (index == 2)
                return MS.INV.Properties.Resources._2;
            else if (index == 3)
                return MS.INV.Properties.Resources._3;
            return null;
        }

        private Image SetImgOpacity(float imgOpac)
        {
            Image imgPic = GetImage();

            Bitmap bmpPic = new Bitmap(imgPic.Width, imgPic.Height);
            Graphics gfxPic = Graphics.FromImage(bmpPic);
            ColorMatrix cmxPic = new ColorMatrix();
            cmxPic.Matrix33 = imgOpac;

            ImageAttributes iaPic = new ImageAttributes();
            iaPic.SetColorMatrix(cmxPic, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            gfxPic.DrawImage(imgPic, new Rectangle(0, 0, bmpPic.Width, bmpPic.Height), 0, 0, imgPic.Width, imgPic.Height, GraphicsUnit.Pixel, iaPic);
            gfxPic.Dispose();

            return bmpPic;
        }

        GraphicsPath GetStringPath(string s, float dpi, RectangleF rect, Font font, StringFormat format)
        {
            GraphicsPath path = new GraphicsPath();
            // Convert font size into appropriate coordinates
            float emSize = dpi * font.SizeInPoints / 72;
            path.AddString(s, font.FontFamily, (int)font.Style, emSize, rect, format);
            
            return path;
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (img_Help.Contains(e.X, e.Y) || img_Supp.Contains(e.X, e.Y) || img_logout.Contains(e.X, e.Y))
                this.Cursor = Cursors.Hand;
            else
                this.Cursor = Cursors.Default;

            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (img_Help.Contains(e.X, e.Y))
                Lib.Utils.Uti.RunFile(Application.StartupPath + @"\Helps\Helps.exe");
            if (img_Supp.Contains(e.X, e.Y))
            {
                if (SupportClicked != null)
                    SupportClicked(null, new EventArgs());
            }
            if (img_logout.Contains(e.X, e.Y))
                Application.Restart();

            base.OnMouseDown(e);
        }
    }
}
